/**
 * The Prescription class will take in the drugs and instructions given by the doctor
 * for the patient to look at.
 */
package docoffice.doss;

import java.util.logging.Logger;
import java.sql.ResultSet;

import docoffice.database.DatabaseManager;


/**
 * @author Jackie Trujillo
 * @version $Revision 1.0
 */
public class Prescription{
	
	/**
	 * Field drugs.
	 */
	private String drugs;
	
	/**
	 * Field instructions.
	 */
	private String instructions;
	
	/**
	 * Field id.
	 */
	private int id;
	
	/**
	 * Field LOG.
	 */
	private static final Logger LOG = Logger.getLogger("Prescription");
	
	/**
	 * Constructor for Prescription.
	 * @param dDrugs String
	 * @param iInstructions String
	 */
	public Prescription(String dDrugs, String iInstructions){
		this.drugs = dDrugs;
		this.instructions = iInstructions;
	}

	/**
	 * Method getDrugs.
	
	 * @return String */
	public String getDrugs() {
		return drugs;
	}

	/**
	 * Method setDrugs.
	 * @param drugs String
	 */
	public void setDrugs(String drugs) {
		this.drugs = drugs;
	}

	/**
	 * Method getInstructions.
	
	 * @return String */
	public String getInstructions() {
		return instructions;
	}

	/**
	 * Method setInstructions.
	 * @param instructions String
	 */
	public void setInstructions(String instructions) {
		this.instructions = instructions;
	}

	/**
	 * Method getId.
	 * @return int
	 */
	public int getId(){
		return this.id;
	}
	
	/**
	 * Method setId.
	 * @param id int
	 */
	public void setId(int id){
		this.id = id;
	}
	
	/**
	 * Method toString.
	
	 * @return String */
	public String toString(){
		return "This class handles prescriptions";
	}
	
	/** 
	 * createPrescription method
	 * @param medicine String
	 * @param instructions String
	 **/
	public void createPrescriptoin(String medicine, String instructions){
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "INSERT INTO prescriptions (Medicine, Instructions)" +
				" VALUES ('" + medicine + "', '" + instructions + "')";
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while creating prescription");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("Prescription successfully created");
	}
	
	/**
	 * Method readPrescription.
	 * @param prescrip Prescription
	 * @return Prescription
	 */
	public Prescription readPrescription(Prescription prescrip){
		final int id = prescrip.getId();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "SELECT * FROM prescriptions WHERE " +
				"PrescriptionID = " + id;
		ResultSet rs = null;
		
		try{
			rs = dbMgr.runReadQuery(sqlQuery);
			rs.first();
			final String medicine = rs.getString("Medicine");
			final String instructions = rs.getString("Instructions");
			
			prescrip.setDrugs(medicine);
			prescrip.setInstructions(instructions);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while reading prescription info");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection(rs);
		}
		
		LOG.info("Prescription info read successfully");
		return prescrip;
	}
	
	public int readPrescriptionID(Prescription prescrip){
		int id = 0;
		final String med = prescrip.getDrugs();
		final String instr = prescrip.getInstructions();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "SELECT PrescriptionID FROM prescriptions WHERE" +
				" Medicine = '" + med + "' AND Instructions = '" + instr + "'";
		ResultSet rs = null;
		
		try{
			rs = dbMgr.runReadQuery(sqlQuery);
			rs.first();
			id = rs.getInt("PrescriptionID");
		} catch(Exception e){
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection(rs);
		}
		
		return id;
	}
	
	public void updatePrescription(Prescription prescription){
		final int id = prescription.readPrescriptionID(prescription);
		final String drugs = prescription.getDrugs();
		final String instructions = prescription.getInstructions();
		
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "UPDATE prescriptions SET Medicine = '" + 
		        drugs + "', Instructions = '" + instructions + "' WHERE" +
		        " PrescriptionID = " + id; 
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while updating prescription");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("Prescription successfully updated");
	}
	
	public void deletePrescription(Prescription prescription){
		final int id = prescription.readPrescriptionID(prescription);
		
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "DELETE FROM prescriptions WHERE" +
		        " PrescriptionID = " + id; 
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while deleting prescription");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("Prescription successfully deleted");
	}
}
